package com.hhu.wangzb.server.service.impl;

import com.hhu.wangzb.common.security.constant.SecurityConstants;
import com.hhu.wangzb.shiro.jwt.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhu.wangzb.common.utils.PageUtils;
import com.hhu.wangzb.common.utils.Query;

import com.hhu.wangzb.server.dao.UserDao;
import com.hhu.wangzb.common.entity.UserEntity;
import com.hhu.wangzb.server.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserEntity queryByParamsMap(Map<String, Object> paramsMap) {

        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(paramsMap);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public UserEntity queryAuthenticated(String token) {
        Integer userId = JwtUtils.getInt(token, SecurityConstants.S_SESSION_USER_ID);

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("id", userId);

        return queryByParamsMap(queryMap);
    }

    @Override
    public boolean save(UserEntity entity) {


        return super.save(entity);
    }
}